<?php

/**
 * 
 * @author yuxun
 *
 */
class ActivityController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('大活动管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$classifyList = ActivityCategoryService::classifyList();
		$classifyList = array('' => '所有') + $classifyList;
	
		$filter->addFilter('classify', 'select', PubFunc::t('种类'), array('options' => $classifyList, 'value' => key($classifyList)));
		$filter->addFilter('activity_id', 'input', '活动ID');
		$filter->addFilter('activity_name', 'input', '活动名称');

	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ActivityService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ActivityService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=activity/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'activity/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
			
			$user = AccAuth::me();
			if($user['Identity'] == 2 || $user['UserID'] == 1){ //可以操作废弃和关闭
				$this->out['is_close'] = 1;
			}else{
				$this->out['is_close'] = 2;
			}
			
		}
	}
	
	public function add(){
		$this->setTitle('添加活动');
		$classifyList = ActivityCategoryService::classifyList();
		$this->out['classifyList'] = $classifyList;
	}
	
	public function doAdd(){
// 		$count = ActivityService::repeat(0, 'category_id', $this->R('category_id'),$this->R('g'));
// 		if($count > 0){
// 			$this->showMessage('该活动ID已经存在');
// 		}else{
			
// 		}
		$result = ActivityService::doAdd($_POST);
		if($result['state'] !== false){
			$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=activity/index"));
		}else{
			$this->showMessage($result['msg']);
		}
	}
	
	public function edit(){
		$this->setTitle('编辑活动');

		$id = $this->R('id');
		$info = ActivityService::get($id);
		$info['start_time'] = date('Y-m-d H:i',$info['start_time']);
		if(empty($info)) $this->showMessage('找不到该信息');
		$this->out['info'] = $info;
		
		$classifyList = ActivityCategoryService::classifyList();
		$this->out['classifyList'] = $classifyList;
		
		$classify = $info['classify'];
		$game = PubBase::getCurrentGame();
		$where['g'] = $game;
		$where['classify'] = $classify;
		$modActivityCategory = new ModActivityCategory();
		$activityList = $modActivityCategory->select($where);
		$this->out['activityList'] = $activityList;
 		
	}
	
	public function doEdit(){
		$id = $this->R('id');
// 		$count = ActivityService::repeat($id, 'category_id', $this->R('category_id'),$this->R('g'));
// 		if($count > 0){
// 			$this->showMessage('该活动ID已经存在');
// 		}else{
			
// 		}
		$result = ActivityService::doEdit($id, $_POST);
		if($result['state'] !== false){
			$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=activity/index"));
		}else{
			$this->showMessage('编辑失败:'.$result['msg']);
		}
	
	}
	
	public function getActivity(){
		$classify = $this->R('classify');
		$result = ActivityCategoryService::getActivity($classify);
		$this->out = $result;
		$this->outType = 'json';
	}
	
	public function getActivityInfo(){
		$category_id = $this->R('category_id');
		$result = ActivityCategoryService::getActivityInfo($category_id);
		$this->out = $result;
		$this->outType = 'json';
	}
	
	public function delete(){
		$id = $this->R('id');
		if($this->isAuth !=1){  //isAuth为1则没有权限
			$result = ActivityService::delete($id);
			if($result['state']  !== false){
				$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=activity/index"));
			}else{
				$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=activity/index"));
			}
		}		
	}
	
    public function synchro(){
    	$ids = PubRequest::R('ids');
    	if(PubFunc::isAjax()){
    		$servers = PubRequest::R('servers');
    		$channel = PubRequest::R('channel');
    		$cls_channel = PubRequest::R('cls_channel');
    		$result = ActivityService::synchro($ids,$servers,$channel,$cls_channel);
    		$this->out = $result;
    		$this->outType = 'json';
    	}else{
    		$this->out['ids'] = $ids;
    		$this->layout = '';
    
    		$url = PubFunc::buildUrl("/oss/?r=broadcast/synchro");
    		$this->out['url'] = $url;
    		
    		$game = PubBase::getCurrentGame();
    		$list = SpecialServerService::getAllServerChannel($game);
    		$this->out['list'] = $list;
    
    	}
    }
    
    
    public function updateStatus(){
    	$ids = PubRequest::R('ids');
    	$type = PubRequest::R('type');
    	if($ids == ''){
    		$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
    	}else{
    		$out = ActivityService::updateStatus($ids,$type);
    	}
    	
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    /**
     * 查看同步的信息
     */
    public function synchroInfo(){
    	$id = PubRequest::R('id');
    	$synchroInfo = ActivityService::synchroInfo($id);
    	$this->out['synchroInfo'] = $synchroInfo;
    	
    	$info = ActivityService::get($id);
    	$this->out['info'] = $info;
    	
    	$game = PubBase::getCurrentGame();
    	$this->out['g'] = $game;
    	
    	$this->layout = '';
    	
    }


    public function infoUpdate(){
    	$id = PubRequest::R('id');
    	$type = PubRequest::R('type');
    	$out = ActivityService::info_update($id, $type);
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    
}